<template>
  <el-card style="margin: 20px">
    <el-row type="flex" justify="space-between">
      <el-col :span="7">
        <el-row type="flex" justify="start">
          <el-input
            v-model="params.title"
            placeholder="根据用户名搜索"
          ></el-input>
          <el-button style="margin-left: 10px">清空</el-button>
          <el-button type="primary" @click="search">搜索</el-button>
        </el-row>
      </el-col>
      <el-col :span="3">
        <el-button type="success" class="el-icon-edit" @click="adduser"
          >新增权限组</el-button
        >
      </el-col>
    </el-row>

    <el-alert
      style="margin-top: 20px"
      :title="`共${counts}条记录`"
      type="info"
      :closable="false"
      show-icon
    >
    </el-alert>
    <!-- 表格 -->
    <el-table style="margin-top: 20px" :data="userlist">
      <el-table-column type="selection" width="55"> </el-table-column>

      <el-table-column prop="title" label="用户名"> </el-table-column>

      <el-table-column prop="create_date" label="日期"> </el-table-column>
      <el-table-column prop="date" label="操作">
        <template #default="{ row }">
          <el-button
            icon="el-icon-edit"
            circle
            type="primary"
            plain
            @click="changeuser(row,row.id)"
          ></el-button>
          <el-button
            icon="el-icon-delete"
            circle
            type="danger"
            plain
            @click="removeUser(row, row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block" style="margin: 25px">
      <el-pagination
        background
        @size-change="params.pagesize"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage3"
        :page-sizes="[10, 20, 30, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="counts"
      >
      </el-pagination>
    </div>

    <!-- 弹框 -->

    <el-dialog
      @close="handleClose"
      :title="title"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose"
    >
      <!-- 新增 -->
      <div v-if="addView">
        <el-form v-model="addForm" label-width="120px" ref="formRef">
          <el-form-item label="用户名">
            <el-input v-model="addForm.title"></el-input>
          </el-form-item>
          <el-tree
            ref="tree"
            :data="treeData"
            :props="{ label: 'title', children: 'childs' }"
            node-key="id"
            default-expand-all
            show-checkbox
          ></el-tree>
        </el-form>
      </div>

      <!-- 修改 -->
      <div v-if="changeView">
        <el-form v-model="form" label-width="120px" ref="formRef">
          <el-form-item label="用户名">
            <el-input v-model="form.username"></el-input>
          </el-form-item>
          <el-tree
            ref="tree"
            :data="treeData"
            :props="{ label: 'title', children: 'childs' }"
            node-key="id"
             :default-checked-keys="checkedList"
            default-expand-all
            show-checkbox
          ></el-tree>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import { list, add, simple, remove,detail } from "@/api/base/permissions";
import { listMenus } from "@/api/base/menus";
export default {
  data() {
    return {
      checkedList:[],
      addForm: {
        title: "",
      },
      defaultProps: {
        children: "children",
        label: "label",
      },
      treeData: [],
      id: null,
      changeView: false,
      title: "",
      addView: false,
      options: [],
      keyword: "",
      form: {
        title: "",
        avatar: null,
        email: "",
        password: "",
        username: "",
        phone: "",
        introduction: "",
        role: "",
        permission_group_id: "",
      },
      dialogVisible: false,
      currentPage3: null,
      username: "",
      pages: "",
      input: "",
      counts: null,
      userlist: [],
      params: {
        page: 1,
        pagesize: 10,
        title: "",
      },
    };
  },
  methods: {
    //列表
    async list() {
      const { data } = await list(this.params);
      // console.log(data);
      this.userlist = data.list;
      this.counts = data.counts;
      this.pages = data.pages;
    },
    //分页
    handleCurrentChange(current) {
      this.params.page = current;
      this.list();
    },
    //搜索
    async search() {
      await list(this.params);
      // console.log(1234565);
      this.list();
    },
    //添加
    async adduser() {
      this.dialogVisible = true;
      this.addView = true;
      this.changeView = false;
      this.title = "添加权限";
      const { data } = await listMenus();

      const arr = JSON.parse(JSON.stringify(data).replace(/points/g, "childs"));
      this.treeData = arr;
    },
    //修改
    async changeuser(row,id) {
      console.log(id);
      // console.log(row);
      this.form = row;
      this.id = row.id;
      this.id = id;
      this.dialogVisible = true;
      this.addView = false;
      this.changeView = true;
      this.title = "编辑权限";
      const { data } = await detail({id:this.id});
      
      this.checkedList = data.permissions
    },
    //删除
    async removeUser(row, id) {
      console.log(id);
      const res = await remove(row, id);
      this.$message.success("删除用户成功");
      console.log(res);
      this.list();
    },
    //清空页面
    handleClose() {
      this.dialogVisible = false;
      this.$refs.formRef.resetFields();
      this.form = {};
    },
    //弹框确认
    async submit() {
      console.log(this.$refs.tree.getCheckedKeys());
      console.log(111);
      if (this.title === "添加权限") {
        await add({
          permissions: this.$refs.tree.getCheckedKeys(),
          title: this.addForm.title,
        });
        this.$message.success("添加权限成功");
      } else {
        await list(this.form, this.id);
        this.$message.success("编辑权限成功");
      }
      this.dialogVisible = false;
      this.list();
    },
  },
  created() {
    this.list();
  },
};
</script>

<style scoped lang="scss"></style>
